<script setup lang="ts">
import { inject, reactive, watchEffect } from 'vue';
const data: any = inject('data');

interface Item {
  T1: { [key: string]: any } | undefined;
  T2: { [key: string]: any } | undefined
}
const personal = reactive<Item[]>([]),
  company = reactive<Item[]>([]);
watchEffect(() => {
  const list = data.value?.pc_pco_pc_k_list || [];

  if (list.length === 0) return;
  list.forEach(item => {
    if (item.PC02KD01 == "自然人") {
      const i = getIndex(personal)
      if (item.PC02KD02 == "担保责任") {
        handleItem(i, 'T1', item, personal)
      } else {
        handleItem(i, 'T2', item, personal)
      }
    } else {
      const i = getIndex(company)
      if (item.PC02KD02 == "担保责任") {
        handleItem(i, 'T1', item, company)
      } else {
        handleItem(i, 'T2', item, company)
      }
    }
  })
})
function handleItem(i: number, type: 'T1' | 'T2', item: any, main: any) {
  const obj = {
    T1: undefined,
    T2: undefined
  }
  obj[type] = item
  if (main[i]) {
    main[i][type] = item
  } else {
    main[i] = obj
  }
}
function getIndex(main: any) {
  const last = Math.max(main.length - 1, 0)
  if (main[last]?.T1 && main[last]?.T2) {
    return last + 1
  }
  return last
}

</script>
<template>
  <div class="h3 title">（3）信贷交易授信及负债信息概要（未结清/未销户）</div>

  <table>
    <caption>
      非循环贷账户信息汇总
    </caption>
    <tbody>
      <tr>
        <th>管理机构数</th>
        <th>账户数</th>
        <th>授信总额</th>
        <th>余额</th>
        <th>最近 6 个月平均应还款</th>
      </tr>
      <tr>
        <td>{{ data?.pc_pco_pce_obj?.PC02ES01 }}</td>
        <td>{{ data?.pc_pco_pce_obj?.PC02ES02 }}</td>
        <td>{{ data?.pc_pco_pce_obj?.PC02EJ01 }}</td>
        <td>{{ data?.pc_pco_pce_obj?.PC02EJ02 }}</td>
        <td>{{ data?.pc_pco_pce_obj?.PC02EJ03 }}</td>
      </tr>
    </tbody>
  </table>

  <table>
    <caption>
      循环贷账户一信息汇总
    </caption>
    <tbody>
      <tr>
        <th>管理机构数</th>
        <th>账户数</th>
        <th>授信总额</th>
        <th>余额</th>
        <th>最近 6 个月平均应还款</th>
      </tr>
      <tr>
        <td>{{ data?.pc_pco_pcf_obj?.PC02FS01 }}</td>
        <td>{{ data?.pc_pco_pcf_obj?.PC02FS02 }}</td>
        <td>{{ data?.pc_pco_pcf_obj?.PC02FJ01 }}</td>
        <td>{{ data?.pc_pco_pcf_obj?.PC02FJ02 }}</td>
        <td>{{ data?.pc_pco_pcf_obj?.PC02FJ03 }}</td>
      </tr>
    </tbody>
  </table>

  <table>
    <caption>
      循环贷账户二信息汇总
    </caption>
    <tbody>
      <tr>
        <th>管理机构数</th>
        <th>账户数</th>
        <th>授信总额</th>
        <th>余额</th>
        <th>最近 6 个月平均应还款</th>
      </tr>
      <tr>
        <td>{{ data?.pc_pco_pcg_obj?.PC02GS01 }}</td>
        <td>{{ data?.pc_pco_pcg_obj?.PC02GS02 }}</td>
        <td>{{ data?.pc_pco_pcg_obj?.PC02GJ01 }}</td>
        <td>{{ data?.pc_pco_pcg_obj?.PC02GJ02 }}</td>
        <td>{{ data?.pc_pco_pcg_obj?.PC02GJ03 }}</td>
      </tr>
    </tbody>
  </table>
  <table>
    <caption>
      贷记卡账户信息汇总
    </caption>
    <tbody>
      <tr>
        <th>发卡机构数</th>
        <th>账户数</th>
        <th>授信总额</th>
        <th>单家机构最高授信额</th>
        <th>单家机构最低授信额</th>
        <th>已用额度</th>
        <th>最近6个月平均使用额度</th>
      </tr>
      <tr>
        <td>{{ data?.pc_pco_pch_obj?.PC02HS01 }}</td>
        <td>{{ data?.pc_pco_pch_obj?.PC02HS02 }}</td>
        <td>{{ data?.pc_pco_pch_obj?.PC02HJ01 }}</td>
        <td>{{ data?.pc_pco_pch_obj?.PC02HJ02 }}</td>
        <td>{{ data?.pc_pco_pch_obj?.PC02HJ03 }}</td>
        <td>{{ data?.pc_pco_pch_obj?.PC02HJ04 }}</td>
        <td>{{ data?.pc_pco_pch_obj?.PC02HJ05 }}</td>
      </tr>

    </tbody>
  </table>

  <table>
    <caption>
      准贷记卡账户信息汇总
    </caption>
    <tbody>
      <tr>
        <th>发卡机构数</th>
        <th>账户数</th>
        <th>授信总额</th>
        <th>单家机构最高授信额</th>
        <th>单家机构最低授信额</th>
        <th>透支余额</th>
        <th>最近6个月平均使用额度</th>
      </tr>
      <tr>
        <td>{{ data?.pc_pco_pci_obj?.PC02IS01 }}</td>
        <td>{{ data?.pc_pco_pci_obj?.PC02IS02 }}</td>
        <td>{{ data?.pc_pco_pci_obj?.PC02IJ01 }}</td>
        <td>{{ data?.pc_pco_pci_obj?.PC02IJ02 }}</td>
        <td>{{ data?.pc_pco_pci_obj?.PC02IJ03 }}</td>
        <td>{{ data?.pc_pco_pci_obj?.PC02IJ04 }}</td>
        <td>{{ data?.pc_pco_pci_obj?.PC02IJ05 }}</td>
      </tr>

    </tbody>
  </table>

  <table>
    <caption>
      相关还款责任信息汇总
    </caption>
    <tbody>
      <template v-if="personal.length">
        <tr>
          <th colspan="10">为个人</th>
        </tr>
        <tr>
          <th colspan="3">担保责任</th>
          <th colspan="3">其他相关还款责任</th>
        </tr>
        <tr>
          <th>账户数</th>
          <th>担保金额</th>
          <th>余额</th>
          <th>账户数</th>
          <th>还款责任金额</th>
          <th>余额</th>
        </tr>
        <tr v-for="(item, i) in personal" :key="i">
          <td>{{ item.T1?.PC02KS02 }}</td>
          <td>{{ item.T1?.PC02KJ01 }}</td>
          <td>{{ item.T1?.PC02KJ02 }}</td>
          <td>{{ item.T2?.PC02KS02 }}</td>
          <td>{{ item.T2?.PC02KJ01 }}</td>
          <td>{{ item.T2?.PC02KJ02 }}</td>
        </tr>
      </template>
      <template v-if="company.length">
        <tr>
          <th colspan="10">为企业</th>
        </tr>
        <tr>
          <th colspan="3">担保责任</th>
          <th colspan="3">其他相关还款责任</th>
        </tr>
        <tr>
          <th>账户数</th>
          <th>担保金额</th>
          <th>余额</th>
          <th>账户数</th>
          <th>还款责任金额</th>
          <th>余额</th>
        </tr>
        <tr v-for="(item, i) in company" :key="i">
          <td>{{ item.T1?.PC02KS02 }}</td>
          <td>{{ item.T1?.PC02KJ01 }}</td>
          <td>{{ item.T1?.PC02KJ02 }}</td>
          <td>{{ item.T2?.PC02KS02 }}</td>
          <td>{{ item.T2?.PC02KJ01 }}</td>
          <td>{{ item.T2?.PC02KJ02 }}</td>
        </tr>
      </template>
    </tbody>
  </table>
</template>